package com.example.nuc_ms_beta.ui.viewconstruction.moderfragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;
import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.HumanLecture.HumanLecture;
import com.example.nuc_ms_beta.ui.viewconstruction.adapter.HumanLectureListViewAdapter;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 * Created by 张博川 on 2016/9/28. 人文讲座
 */
public class HumanLectureFragment extends Fragment {
    private View view;//主View

    private XRefreshView humanlecture_xrefreshview;
    private ListView humanlecture_listview;
    private static long lastRefreshTime;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return view;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.humanlecturefragment, container, false);
        humanlecture_listview = (ListView) view.findViewById(R.id.humanlecture_listview);
        humanlecture_xrefreshview = (XRefreshView) view.findViewById(R.id.humanlecture_xrefreshview);
        humanlecture_xrefreshview.setPullLoadEnable(true);
        humanlecture_xrefreshview.setPullLoadEnable(true);
        humanlecture_xrefreshview.restoreLastRefreshTime(lastRefreshTime);
        humanlecture_xrefreshview.setMoveHeadWhenDisablePullRefresh(true);
        humanlecture_xrefreshview.setAutoRefresh(false);
        humanlecture_xrefreshview.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        humanlecture_xrefreshview.stopRefresh();
                        lastRefreshTime = humanlecture_xrefreshview.getLastRefreshTime();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        humanlecture_xrefreshview.stopLoadMore();
                    }
                }, 2000);
            }

            @Override
            public void onRelease(float direction) {
                super.onRelease(direction);
                if (direction > 0) {

                } else {

                }
            }

        });
        queryData();

    }

    private void queryData() {
        BmobQuery<HumanLecture> query_listView_top = new BmobQuery<>();
        query_listView_top.findObjects(new FindListener<HumanLecture>() {
            @Override
            public void done(final List<HumanLecture> list, BmobException e) {
                if (e == null) {
                    humanlecture_listview.setAdapter(new HumanLectureListViewAdapter(getContext(), list, R.layout.humanlecture_listview_item));
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });


    }

    public static HumanLectureFragment getInstance() {
        HumanLectureFragment fragment = new HumanLectureFragment();
        return fragment;
    }
}
